local BattleGlobalMgr = Battle.CreateClass("BattleGlobalMgr")
Battle.BattleGlobalMgr = BattleGlobalMgr
local this = BattleGlobalMgr
function BattleGlobalMgr.OnCreate()
    ---加速阶段
    this.isAcc = false
    this.accelerateTime = 0
    this.isWeather = false
    this.weatherStart = 0
    this.weatherId = 0
end
function BattleGlobalMgr.OnDestroy()
    Battle.ClientBattle:RegisterUpdateCallback(this.Update)
end
function BattleGlobalMgr.InitGlobal()
    this.accelerateTime = Battle.GlobalConfig.GetInt(17)
    this.weatherStart = Battle.GlobalConfig.GetInt(27)/1000
end
function BattleGlobalMgr.OnInitialize()
    this.InitGlobal()
    Battle.ClientBattle:RegisterUpdateCallback(this.Update)
end
function BattleGlobalMgr.RandomWeather()
    local params = decodeJsonStrToTable(Battle.GlobalConfig.GetString(26))
    local weight = 0
    for k,v in pairs(params) do
        weight = weight + v[2]
    end
    local w = math.random(0,weight)
    for k,v in ipairs(params) do
        if w <= v[2] then
            this.weatherId = v[1]
            break
        end
        w = w - v[2]
    end
end
function BattleGlobalMgr.GetWeatherScenePath()
    local cfg = DataTable.GetById("t_weather",this.weatherId)
    if cfg == nil then
        logError("找不到配置表t_weather "..this.weatherId)
        return ""
    end
    return cfg.f_Scene
end
function BattleGlobalMgr.Update(dt)
    local stage = Battle.ClientBattle.battleStage
    if stage == Battle.BattleStage.Run then
        if not this.isAcc then
            this.accelerateTime = this.accelerateTime - dt
            if this.accelerateTime <= 0 then
                this.isAcc = true
                Battle.GlobalEventSystem.BroadCast(Battle.Event.AccelerateStart)
                Battle.GlobalBuffSystem.AddAccelerateBuff(Battle.GlobalConfig.GetInt(18))
                Battle.PlayerSystem.AccelerateAllPlayer(Battle.GlobalConfig.GetInt(20)/1000)
            end
        end
        if not this.isWeather then
            this.weatherStart = this.weatherStart - dt
            if this.weatherStart <= 0 then
                this.isWeather = true
                local cfg = DataTable.GetById("t_weather",this.weatherId)
                if not cfg or cfg.f_Type == 1 then return end
                Battle.GlobalEventSystem.BroadCast(Battle.Event.WeatherStart,Battle.EventArgs.WeatherStartArgs:Create(this.weatherId))
                Battle.GlobalBuffSystem.AddWeatherBuff(decodeJsonStrToTable(cfg.f_Buff))
                Battle.EffectShowMgr.PlayEffect(cfg.f_ThemeEffects)
            end
        end

    end

end